package org.kongqian.basic.service.impl;

import org.kongqian.basic.mapper.BaseMapper;
import org.kongqian.basic.query.BaseQuery;
import org.kongqian.basic.service.IBaseService;
import org.kongqian.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void insert(T t) {
        baseMapper.insert(t);
    }

    @Override
    @Transactional
    public void deleteById(Serializable id) {
        baseMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void updateById(T t) {
        baseMapper.updateById(t);
    }

    @Override
    public List<T> selectAll() {
        return baseMapper.selectAll();
    }

    @Override
    public T selectById(Serializable id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageList<T> listPage(BaseQuery query) {
        Integer total = baseMapper.queryTotal(query);
        if (total<1){
            return new PageList();
        }
        List<T> departments = baseMapper.CurrentPageData(query);
        return new PageList(total,departments);
    }
    }
